// pages/monthcount/monthcount.js
var rate = 0;
var canvasWidth = 0;
var canvasHeight = 0;
var util = require('../../utils/util.js');
const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2018-04-13',
    green: 1,
    columnCanvasData: {
      canvasId: 'columnCanvas',
    },
    assetarr: app.server.assetarr
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var time = util.formatTime(new Date());
    time = time.split(' ')[0];
    time = time.split('-');
    var timearr = time[0] + '-' + time[1];
    // 再通过setData更改Page()里面的data，动态更新页面的数据  
    this.setData({
      date: timearr
    });
    // that.showzhuxing();
    that.choosemonth(that.data.date);
    // that.showzhuxing2();
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  choosemonth: function (date) {
    var that = this;
    // 获取成员信息
    var datas = {};
    var year = date.split('-')[0];
    var month = date.split('-')[1];
    var accid = wx.getStorageSync('depid');
    datas.Year = year;
    datas.Month = month;
    datas.AccID = accid;
    console.log(datas)
    var datacall = function (res) {
      console.log(res);
      if (res.data.code == 0 && res.data.data.length != 0) {
        var monthget = [];
        var len = res.data.data.MonthSums.length;
        var totalnum = 0;
        var sumnum = 0;
        var max = 0;
        var datamonth = [];
        for (var i = 0; i < len; i++) {
          monthget[i] = res.data.data.MonthSums[len - i - 1];
        }
        var montharr = [];
        for (var i = 0; i < monthget.length; i++) {
          var months = monthget[i].Month;
          if (months < 10) {
            months = '0' + months;
          }
          montharr[i] = monthget[i].Year + '' + months;

          if (that.data.green == 1) {
            if (max < monthget[i].MnyGetSum) {
              max = monthget[i].MnyGetSum;
            }
            datamonth[i] = monthget[i].MnyGetSum ? monthget[i].MnyGetSum : 0;
          } else {
            if (max < monthget[i].MnyPaySum) {
              max = monthget[i].MnyPaySum;
            }
            datamonth[i] = monthget[i].MnyPaySum ? monthget[i].MnyPaySum : 0;
          }
        }
        max = Math.ceil(max / 100) * 100;
        var kedu = Math.ceil((max / 3) / 100) * 100;
        var bili = 60 / max;
        var ydata = [];
        var iii = {};//装载数据的盒子
        for (var i = 0; i < 4; i++) {
          iii.x = 0;
          iii.y = i * 20;
          iii.title = i * kedu;
          ydata[i] = iii;
          iii = {};
        };
        for (var i = 0; i < datamonth.length; i++) {
          datamonth[i] = datamonth[i] * bili;
        }
        if (that.data.green == 1) {
          totalnum = res.data.data.SumInfo[0].GetCount;
          sumnum = res.data.data.SumInfo[0].MnyGetSum;
        } else {
          totalnum = res.data.data.SumInfo[0].GetCount1;
          sumnum = res.data.data.SumInfo[0].MnyPaySum;
        }
        that.showzhuxing();
        console.log(montharr);
        console.log(ydata);
        console.log(datamonth);
        that.showzhuxing2(montharr, ydata, datamonth);
        var orderarr = [];
        if(that.data.green==0){
          for (var i = 0; i < res.data.data.TypeSums.length; i++) {
            if (res.data.data.TypeSums[i].MnyHap<0){
              res.data.data.TypeSums[i].realnum = res.data.data.TypeSums[i].MnyHap * (-1);
              orderarr.push(res.data.data.TypeSums[i]);
            }
          }
          //冒泡排序
          for (var i = 0; i < orderarr.length - 1; i++) {
            for (var j = i + 1; j < orderarr.length; j++) {
              if (orderarr[i].realnum < orderarr[j].realnum) {//如果前面的数据比后面的小就交换  
                var temp = orderarr[i];
                orderarr[i] = orderarr[j];
                orderarr[j] = temp;
              }
            }
          }
        }else{
          for (var i = 0; i < res.data.data.TypeSums.length; i++) {
            if (res.data.data.TypeSums[i].MnyHap >= 0) {
              orderarr.push(res.data.data.TypeSums[i]);
            }
          }
          for (var i = 0; i < orderarr.length - 1; i++) {
            for (var j = i + 1; j < orderarr.length; j++) {
              if (orderarr[i].MnyHap < orderarr[j].MnyHap) {//如果前面的数据比后面的小就交换  
                var temp = orderarr[i];
                orderarr[i] = orderarr[j];
                orderarr[j] = temp;
              }
            }
          } 
        }
        
        that.setData({
          monthinfo: res.data.data,
          monthget: monthget,
          totalnum: totalnum,
          sumnum: sumnum,
          orderarr: orderarr
        });
      }

    }
    var res2 = post.getdata("/Action/PK01/GetAccMonthSumRpt", datas, datacall);
  },

  showzhuxing: function () {
    var that = this;
    var systemInfo = app.systemInfo;
    rate = systemInfo.screenWidth / 750;
    var updateData = {};
    canvasWidth = systemInfo.screenWidth - rate * 64 - 20;
    canvasHeight = rate * 306 + rate * 44 + rate * 34 + rate * 22;

    var culumnYMax = 60;
    var culumnYMin = 0;
    updateData['columnCanvasData.canvasWidth'] = canvasWidth;
    updateData['columnCanvasData.axisPadd'] = { left: rate * 5, top: rate * 13, right: rate * 5 };
    updateData['columnCanvasData.axisMargin'] = { bottom: rate * 34, left: rate * 26 };
    updateData['columnCanvasData.yAxis.fontSize'] = rate * 22;
    updateData['columnCanvasData.yAxis.fontColor'] = '#637280';
    updateData['columnCanvasData.yAxis.lineColor'] = '#DCE0E6';
    updateData['columnCanvasData.yAxis.lineWidth'] = rate * 2;
    updateData['columnCanvasData.yAxis.dataWidth'] = rate * 42;
    updateData['columnCanvasData.yAxis.isShow'] = true;
    updateData['columnCanvasData.yAxis.isDash'] = true;
    updateData['columnCanvasData.yAxis.minData'] = culumnYMin;
    updateData['columnCanvasData.yAxis.maxData'] = culumnYMax;
    updateData['columnCanvasData.yAxis.padd'] = rate * 306 / (culumnYMax - culumnYMin);

    updateData['columnCanvasData.xAxis.dataHeight'] = rate * 26;
    updateData['columnCanvasData.xAxis.fontSize'] = rate * 22;
    updateData['columnCanvasData.xAxis.fontColor'] = '#637280';
    updateData['columnCanvasData.xAxis.lineColor'] = '#DCE0E6';
    updateData['columnCanvasData.xAxis.lineWidth'] = rate * 2;
    updateData['columnCanvasData.xAxis.padd'] = rate * 52;
    updateData['columnCanvasData.xAxis.dataWidth'] = rate * 42;
    updateData['columnCanvasData.xAxis.leftOffset'] = rate * 40;


    updateData['columnCanvasData.canvasHeight'] = canvasHeight;
    updateData['columnCanvasData.enableScroll'] = true;

    that.setData(updateData);
  },
  showzhuxing2: function (montharr, ydata, datamonth) {
    var that = this;
    var updateData = {};

    var columnYMax = 60;
    var columnYMin = 0;
    updateData['columnCanvasData.yAxis.minData'] = columnYMin;
    updateData['columnCanvasData.yAxis.maxData'] = columnYMax;
    updateData['columnCanvasData.series'] = [{
      data: datamonth,
    }];
    updateData['columnCanvasData.xAxis.data'] = montharr;
    //  ['201711', '201712', '201801', '201802', '201803', '201804'];
    updateData['columnCanvasData.yAxis.data'] = ydata;
    //  [
    //   { x: 0, y: 0, title: '0' },
    //   { x: 0, y: 20, title: '20' },
    //   { x: 0, y: 40, title: '40' },
    //   { x: 0, y: 60, title: '60' }
    // ];

    that.setData(updateData);
  },
  bindDateChange: function (e) {
    var that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      date: e.detail.value
    })
    that.choosemonth(e.detail.value);
  },

  changezhichu: function (e) {
    var that = this;
    that.setData({
      green: 0,
      totalnum: this.data.monthinfo.SumInfo[0].GetCount1,
      sumnum: this.data.monthinfo.SumInfo[0].MnyPaySum
    })
    that.choosemonth(that.data.date);
  },
  changeshouru: function (e) {
    var that = this;
    that.setData({
      green: 1,
      totalnum: this.data.monthinfo.SumInfo[0].GetCount,
      sumnum: this.data.monthinfo.SumInfo[0].MnyGetSum
    })
    that.choosemonth(that.data.date);
  },
  onTouchHandler(e) {
    if (null == this.column_chart) {
      this.column_chart = this.selectComponent("#column-chart");
    }
    this.column_chart.onTouchHandler(e);
  },
  onTouchMoveHandler(e) {
    if (null == this.column_chart) {
      this.column_chart = this.selectComponent("#column-chart");
    }
    this.column_chart.onTouchMoveHandler(e);
  },
  onTouchEndHandler(e) {
    if (null == this.column_chart) {
      this.ccolumn_chart = this.selectComponent("#column-chart");
    }
    this.column_chart.onTouchEndHandler(e);
  },
})